package com.zline.foodraise.ui.activity;

import android.graphics.Bitmap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zline.foodraise.R;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.AppPref_;
import com.zline.foodraise.js.HostJSScope;
import com.zline.foodraise.js.MyWebViewSetting;
import com.zline.foodraise.listener.CanChildScrollUpCallback;
import com.zline.foodraise.listener.JsInvokeListener;
import com.zline.foodraise.util.LUtil;
import com.zline.foodraise.util.StringUtils;
import com.zline.foodraise.util.URLUtils;
import com.zline.foodraise.view.ScrollSwipeRefreshLayout;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.json.JSONObject;

/**
 * Created by heshantao on 15/10/19.
 * 支付失败页面
 */
@EActivity(R.layout.activity_web)
public class PayFailActivity extends ZlineActivity implements JsInvokeListener ,CanChildScrollUpCallback {
    private static final String LOG_TAG=PayFailActivity.class.getSimpleName();
    @ViewById(R.id.activity_web_wv)
    WebView wv;
    @ViewById(R.id.activity_web_srl)
    ScrollSwipeRefreshLayout swipeRefreshLayout;
    @ViewById(R.id.app_common_header_title_tv)
    TextView title_tv;
    MyWebViewSetting mSetting;
    @Bean
    HostJSScope hostJSScope;
    @Bean
    URLUtils urlUtils;
    @StringRes(R.string.order_create)
    String title;
    @Extra
    String url;
    @Pref
    AppPref_ appPref;
    @Extra
    String erroMsg;
    @ViewById(R.id.exception_rl)
    RelativeLayout exception_rl;
    @ViewById(R.id.exception_fl)
    FrameLayout exception_fl;
    //点击重试
    boolean isReLoad=false;
    //加载出错
    boolean isErro=false;

    @AfterViews
    void doSomeAfterViews() {
        if(StringUtils.isBlank(url)){
        url=appPref.rootUrl().get()+ Constant.PAY_RESULT_FAIL_URL+
                "?"+"title=订单支付失败"+"&orderCode="+appPref.orderCode().get()
                +"&erroMsg="+erroMsg+"&isEmbed=1";
        }
        initWebView();
        initHeader();
        initSwipRefreshLayout();
    }

    void initHeader(){
        title_tv.setText(title);
    }


    void initWebView() {
        mSetting = new MyWebViewSetting(wv, this);
        mSetting.setWebView();
        hostJSScope.setJsInvokeListener(this);
        wv.loadUrl(url);
        wv.setWebViewClient(new WebViewClient() {
            //出错界面
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                showErroView();
                LUtil.i(LOG_TAG, "______________erroCode________" + description);
            }


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                LUtil.i(LOG_TAG, "_________shouldOverrideUrlLoading________" + url);
                doLinkUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                LUtil.i(LOG_TAG, "____Host_____onPageFinished________" + url);
                if (!view.getSettings().getLoadsImagesAutomatically()) {
                    view.getSettings().setLoadsImagesAutomatically(true);
                }
                if (isReLoad && !isErro) {
                    exception_fl.setVisibility(View.GONE);
                    exception_rl.setVisibility(View.GONE);
                    wv.setVisibility(View.VISIBLE);
                }
                isErro = false;
                isReLoad = false;
                stopRefresh();
            }
        });
    }

    //初始化SwipRefreshLayout
    void initSwipRefreshLayout() {
        //设置加载颜色
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light, android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        swipeRefreshLayout.setCanChildScrollUpCallback(this);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                wv.reload();
            }
        });
    }

    //停止刷新
    void stopRefresh() {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
    }


    //显示网页加载出错情况
    void showErroView(){
        isErro=true;
        exception_fl.setVisibility(View.VISIBLE);
        exception_rl.setVisibility(View.VISIBLE);
        wv.setVisibility(View.GONE);
    }

    //处理带跳转链接的url
    void doLinkUrl(String url) {
        String goal = urlUtils.getUrlJumpTo(url);
        LUtil.i(LOG_TAG, "_____url________" + url);
        if (goal.equals("pay-fail")) {//重新加载筹详情页
            wv.loadUrl(url);
        }else if (goal.equals("cfOrder-list")) {//筹订单列表
            ProjectOrderActivity_.intent(this).start();
        }else if(goal.equals("ccOrder-list")){//商品订单列表
            ProductOrderActivity_.intent(this).start();
        }

    }

    @Override
    public void jsInvoke(int flag) {
        switch (flag) {
            case Constant.WEB_JUMP_HOME_PAGE:
                MainActivity_.intent(this).start();
                break;
            case Constant.WEB_JUMP_EAT_PAGE:
                MainActivity_.intent(this).start();
                break;
            default:
                break;
        }

    }

    @Override
    public void jsInvoke(int flag, JSONObject jsonObject) {

    }

    @Override
    public void jsInvoke(double marginTop) {

    }

    @Override
    public void goback() {
        super.goback();
        MainActivity_.intent(this).start();

    }

    @Click({R.id.app_common_header_left_icon_iv,R.id.reload_tv})
    void OnClick(View view) {
        switch (view.getId()) {
            case R.id.app_common_header_left_icon_iv:
                goback();
                break;
            case R.id.reload_tv:
                isReLoad=true;
                wv.reload();
                break;
            default:
                break;
        }
    }



    @Override
    protected void onResume() {
        super.onResume();
        hostJSScope.setJsInvokeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        hostJSScope.setJsInvokeListener(null);
    }

    @Override
    public boolean canSwipeRefreshChildScrollUp() {
        return wv.getScrollY()> Constant.WEBVIEW_SCROLL_Y;
    }
}
